package controller

import (
	"chitchat_http/model"
	"chitchat_http/server"
	"chitchat_http/util"
	"github.com/doujunyu/gogo/job"
)

// ChatByApplyFriendOrApplyGroup 申请加好友，进群，邀请好友进群
func ChatByApplyFriendOrApplyGroup(j *job.Job){
	ReceivedId := util.StringToInt64(j.Input["received_id"])
	groupId := util.StringToInt64(j.Input["group_id"])
	if ReceivedId == 0{
		j.JsonError(nil,"缺少参数")
		return
	}
	var err error
	switch util.StringToInt64(j.Input["type_id"]) {
	case model.ChatApplyFriendGroupByTypeIdFriend:
		err = server.ChatByApplyFriend(j.GroupData["user_id"].(int64),ReceivedId,j.Input["content"])
		break
	case model.ChatApplyFriendGroupByTypeIdGroup:
		err = server.ChatByApplyGroup(j.GroupData["user_id"].(int64),ReceivedId,j.Input["content"])
		break
	case model.ChatApplyFriendGroupByTypeIdGroupReverse:
		if groupId == 0{
			j.JsonError(nil,"缺少参数")
			return
		}
		err = server.ChatByApplyGroupReverse(j.GroupData["user_id"].(int64),ReceivedId,groupId,j.Input["content"])
	default:
		j.JsonError(nil,"没有对应操作")
		return
	}
	if err != nil {
		j.JsonError(nil,err.Error())
		return
	}
	j.JsonSuccess()
	return
}

// ChatByApplyList 申请列表
func ChatByApplyList(j *job.Job){
	data := server.ChatByApplyList(j.GroupData["user_id"].(int64),j.Input["page"],j.Input["page_size"])
	if len(data) == 0 {
		j.JsonSuccess()
		return
	}
	j.JsonSuccess(data)
	return
}

// ChatByApplyCheck 操作申请信息
func ChatByApplyCheck(j *job.Job){
	ApplyId := util.StringToInt64(j.Input["apply_id"])
	status := util.StringToInt64(j.Input["status"])
	if ApplyId == 0 || status == 0{
		j.JsonError(nil,"缺少参数")
		return
	}
	err := server.ChatByApplyCheck(j.GroupData["user_id"].(int64),ApplyId,status,j.Input["content"])
	if err != nil {
		j.JsonError(nil,err.Error())
		return
	}
	j.JsonSuccess()
	return
}

// ChatByFriendList 好友列表
func ChatByFriendList(j *job.Job){
	data := server.ChatByFriendList(j.GroupData["user_id"].(int64),j.Input["page"],j.Input["page_size"])
	if len(data) == 0 {
		j.JsonSuccess()
		return
	}
	j.JsonSuccess(data)
	return
}

// ChatBySendMessage 发送消息
func ChatBySendMessage(j *job.Job){
	typeId := util.StringToInt64(j.Input["type_id"])
	receivedId := util.StringToInt64(j.Input["received_id"])
	err := server.ChatBySendMessage(typeId,j.GroupData["user_id"].(int64),receivedId,j.Input["content"])
	if err != nil {
		j.JsonError(nil,err.Error())
		return
	}
	j.JsonSuccess()
	return
}

// ChatByCreateGroup 创建群组
func ChatByCreateGroup(j *job.Job){
	if j.Input["name"] == "" || j.Input["introduce"] == ""{
		j.JsonError(nil,"缺少参数")
		return
	}
	data,err := server.ChatByCreateGroup(j.GroupData["user_id"].(int64),j.Input["name"],j.Input["introduce"])
	if err != nil {
		j.JsonError(nil,err.Error())
		return
	}
	j.JsonSuccess(data)
	return
}

// ChatByCleanMessage 消除接收到得消息
func ChatByCleanMessage(j *job.Job){
	userMessageId := util.StringToInt64(j.Input["user_message_id"])
	err := server.ChatByCleanMessage(j.GroupData["user_id"].(int64),userMessageId)
	if err != nil {
		j.JsonError(nil,err.Error())
		return
	}
	j.JsonSuccess()
	return


}


